//
//  DropDownBackView.m
//  Runone
//
//  Created by runone02 on 16/8/22.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "DropDownBackView.h"
#import "UIView+Addition.h"

@interface DropDownBackView ()
@property (nonatomic, weak) UIView *containerView;
@end

@implementation DropDownBackView


- (UIView *)containerView
{
    if (!_containerView) {
        UIView *containerView = [[UIView alloc] init];
        containerView.userInteractionEnabled = YES;
        [self addSubview:containerView];
        self.containerView = containerView;
    }
    return _containerView;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
    }
    return self;
}


+ (instancetype)menu
{
    return [[self alloc] init];
}

- (void)setContent:(UIView *)content
{
    _content = content;

    content.x = 10;
    content.y = 15;

    self.containerView.height = CGRectGetMaxY(content.frame) + 11;

    self.containerView.width = CGRectGetMaxX(content.frame) + 10;

    [self.containerView addSubview:content];
}

- (void)setContentController:(UIViewController *)contentController
{
    _contentController = contentController;
    
    self.content = contentController.view;
}

- (void)showFrom:(UIView *)from
{
   
    UIWindow *window = [[UIApplication sharedApplication].windows lastObject];

    [window addSubview:self];
 
    self.frame = window.bounds;
 
    CGRect newFrame = [from convertRect:from.bounds toView:window];
    self.containerView.x = newFrame.origin.x - 10;
    self.containerView.y = CGRectGetMaxY(newFrame) - 10;
//
//    self.containerView.centerX = from.centerX;
//    self.containerView.y = CGRectGetMaxY(from.frame);
}

- (void)dismiss
{
    [self removeFromSuperview];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self dismiss];
}
@end
